package cn.wx.scholar;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.csvreader.CsvReader;

import cn.wx.common.utils.JSONUtils;

public class ReadCsvExpertFos {

	public static void main(String[] args) {
		
		Map<String,String> fosMap = new HashMap<>();
		final String filePath = "D:\\my_read_csv\\expertFos.csv";
		try {
			
			
			/**
			 * 读取领域信息
			 */
			File file1 = new File("D:\\my_read_csv\\field.csv");
	        FileInputStream fileInputStream1 = new FileInputStream(file1);  
	        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
			
			CsvReader csvReader1 = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
			csvReader1.readHeaders();
			
			while (csvReader1.readRecord()){
				
				/**
				 * 解析csv
				 */
				String fosId = csvReader1.get(0);
				String fosName = csvReader1.get(1);
				
				fosMap.put(fosName.toLowerCase(), fosId);
				
			}
			
			File file = new File(filePath);
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			
			CsvReader csvReader = new CsvReader(bufferedInputStream,Charset.forName("utf-8"));
			StringBuffer sb = null;
			BufferedWriter output = null;
			// 读表头
            csvReader.readHeaders();
            
            int i = 0;
            int zj = 0;
            while (csvReader.readRecord()){
            	
            	if(i == 0) {
            		File of = new File("D:\\my_read_csv\\zj\\expertFos_"+ zj+".csv");
        			FileOutputStream ofs = new FileOutputStream(of,true);
        			OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
        			output = new BufferedWriter(ofw);
        			
        			sb = new StringBuffer();
					sb.append(JSONUtils.c("expertId")).append(",").append(JSONUtils.c("fosName"));
					
					output.write(sb.toString());
					output.newLine();
        			
            	}
            	
                // 读一整行
            	String line = csvReader.getRawRecord();
				System.out.println("读取" + (i++) +"行");
				if(StringUtils.isBlank(line)) {
					continue;
				}

				/**
				 * 解析csv
				 */
				String expertId = csvReader.get(0);
				String fos = csvReader.get(2);
				
				
				if(StringUtils.isNotBlank(fos)) {
					
					@SuppressWarnings("unchecked")
					List<String> arrayFos = JSONUtils.p(fos, List.class);
					
					for(String s:arrayFos) {
						
						if(StringUtils.isBlank(s)) {
							continue;
						}
						
						
						String fosId=fosMap.get(s.toLowerCase());
						
						if(StringUtils.isBlank(fosId)) {
							continue;
						}
						
						sb = new StringBuffer();
						sb.append(expertId).append(",").append(JSONUtils.c(fosId));
						
						output.write(sb.toString());
						output.newLine();
					}
					output.flush();
				}
				
				if(i>10000) {
					i=0;
					zj++;
				}
				
            }
			
            output.flush();
            output.close();
            output = null;
            
            /**
			 * 关闭读入流
			 */
			csvReader.close();
			csvReader = null;
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
